package org.fuxia.im.gui;

import java.awt.Component;
import java.awt.Image;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JTree;

import org.fuxia.im.common.IconManager;
import org.jvnet.substance.api.renderers.SubstanceDefaultTreeCellRenderer;

public class MyTreeCellRender extends SubstanceDefaultTreeCellRenderer {

	private static final long serialVersionUID = 1L;

	public Component getTreeCellRendererComponent(JTree tree, Object value,
			boolean sel, boolean expanded, boolean leaf, int row,
			boolean hasFocus) {
		 super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf,
					row, hasFocus);

		if(value.getClass().isAssignableFrom(MyDefaultMutableTreeNode.class)){
			
			 if(leaf){
				MyDefaultMutableTreeNode myNode = (MyDefaultMutableTreeNode)value;
				if(myNode.getLevel() == 1) {
					if(expanded) {
						setIcon(IconManager.getIcon("images/expanded.png"));
					}else {
						setIcon(IconManager.getIcon("images/menu_arrow.png"));
					}
				}else if(myNode.getLevel() == 2) {
					String url = myNode.getImageUrl() ;
					if(url!=null){
						URL u = Thread.currentThread().getContextClassLoader().getResource(url) ;
						int width = 30 ;
						int height = 30 ;
						if(u!=null){
							ImageIcon icon = new ImageIcon(u) ;
							icon.setImage(icon.getImage().getScaledInstance(width, height, Image.SCALE_DEFAULT)) ;
							setIcon(icon);
						}
					}else {
						if(myNode.getIsGroupNode()) {
							setIcon(IconManager.getIcon("images/manage1.jpg")) ;
						}
					}
				}

			}else{
				if(expanded) {
					setIcon(IconManager.getIcon("images/expanded.png"));
				}else {
					setIcon(IconManager.getIcon("images/menu_arrow.png"));
				}
			}
		}

		return this;
	}

	
	
}
